package jp.michi.makimono;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.util.Log;
import android.view.SurfaceHolder;

public class Page {
    protected File mFile;
    protected int mDataWidth;
    protected int mDataHeight;
    protected int mZoomWidth;
    protected int mZoomHeight;
    //    protected List<List<Bitmap>>
    public static Page newPage(File file) {
        Page page = new Page();
        page.mFile = file;
        return page;
    }
    protected boolean init() {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(mFile, "r");
            Options options = new Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(randomAccessFile.getFD(), null, options);
            if (options.outWidth > 0 && options.outHeight > 0) {
                mDataWidth = options.outWidth;
                mDataHeight = options.outHeight;
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            Log.v("newPage", "", e);
            return false;
        } finally {
            Closer.close(randomAccessFile);
        }
    }
    public void setZoomWidth(int zoomWidth) {
        mZoomWidth = zoomWidth;
        mZoomHeight = mDataHeight * zoomWidth / mDataWidth;
    }
    public void setZoomHeight(int zoomHeight) {
        mZoomWidth = mDataWidth * zoomHeight / mDataHeight;
        mZoomHeight = zoomHeight;
    }
    public void tttt(SurfaceHolder mSurfaceHolder) {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(mFile, "r");
            Options options = new Options();
            options.inInputShareable = true;
            options.inPurgeable = true;
            options.inPreferredConfig = Config.RGB_565;
            final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(randomAccessFile.getFD(), null, options);
            //                builder.append("\nsize " + bitmap.getWidth() + "x" + bitmap.getHeight());
            //                canvas.drawBitmap(bitmap, -options.outWidth / 4, -options.outHeight / 4, paint);
            //                {
            //                    canvas.scale(zoom * 3, zoom * 3);
            //                    canvas.drawBitmap(bitmap, 0, 0, paint);
            //                }
            //                {
            //                    ResizableBitmap resizableBitmap = new ResizableBitmap(bitmap);
            //                    resizableBitmap.resize((3 * zoom * options.outWidth), (3 * zoom * options.outHeight));
            //                    resizableBitmap.draw(canvas);
            //                }
            //                {
            //                    Bitmap zoomBitmap = Bitmap.createScaledBitmap(bitmap, (int) (4 * zoom * options.outWidth), (int) (4 * zoom * options.outHeight), false);
            //                    canvas.drawBitmap(zoomBitmap, 0, 0, paint);
            //                    zoomBitmap.recycle();
            //                }
            {
                //                        final Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
                final Bitmap scaledBitmap = bitmap;
                final Paint paint = new Paint();
                paint.setFilterBitmap(true);
                //                        final Matrix matrix = new Matrix();
                //                        matrix.setScale(zoom, zoom);
                withCanvas(mSurfaceHolder, new CanvasEditor() {
                    @Override
                    public void edit(Canvas canvas) {
                        //                                                        canvas.drawBitmap(bitmap, matrix, paint);
                        canvas.drawBitmap(scaledBitmap,//
                                new Rect(scaledBitmap.getWidth() / 6, scaledBitmap.getHeight() / 6, canvas.getWidth(), canvas.getHeight()),
                                new Rect(0, 0, canvas.getWidth(), canvas.getHeight()),
                                paint);
                    }
                });
            }
            //                {
            //                    canvas.drawBitmap(bitmap,//
            //                            new Rect(0, 0, options.outWidth, options.outHeight),
            //                            new RectF(0, 0, 4 * zoom * options.outWidth, 4 * zoom * options.outHeight),
            //                            paint);
            //                }
            bitmap.recycle();
            randomAccessFile.close();
            //                builder.append("\n");
            //                }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //                runOnUiThread(new Runnable() {
        //                    @Override
        //                    public void run() {
        //                        mConsole.append("size=" + pageLayout.mPages.size() + "\n");
        //                        mConsole.append("load=" + BigDecimal.valueOf(System.nanoTime() - time).movePointLeft(6) + "\n");
        //                    }
        //                });
    }
    protected void withCanvas(SurfaceHolder surfaceHolder, CanvasEditor canvasEditor) {}
    @SuppressWarnings("unused")
    public void drawBitmap(Canvas canvas, int srcX, int srcY, int dstX, int dstY, int width, int height) {
    //        canvas.
    }
}
